package ahblue.cn.tanzi.ui.circle;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.data.source.ApiCallback;
import ahblue.cn.tanzi.data.source.RemoteRepository;
import ahblue.cn.tanzi.model.circle.Circle;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import ahblue.cn.tanzi.util.GlideApp;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CircleToplistActivity extends BaseActivity {

    @BindView(R.id.recyclerViewCircleToplist)
    RecyclerView mRecyclerViewCircleTopList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_circle_toplist);

        ButterKnife.bind(this);

        initData();
    }

    private void initData() {
        RemoteRepository repo = RemoteRepository.getsInstance();
        repo.getBigShotCircleCircleSort(mCallbackCircleRanking);
    }

    @OnClick(R.id.btn_back)
    void onClickButtonBack() {
        finish();
    }




    private ApiCallback<List<Circle>> mCallbackCircleRanking = new ApiCallback<List<Circle>>() {
        @Override
        public void onSuccess(List<Circle> data) {
            Collections.sort(data, new Comparator<Circle>() {
                @Override
                public int compare(Circle o1, Circle o2) {
                    if (o2.member_count == o1.member_count) {
                        if (o2.dynamic_count == o1.dynamic_count) {
                            return o2.curriculum_count - o1.curriculum_count;
                        }
                        return o2.dynamic_count - o1.dynamic_count;
                    }
                    return o2.member_count - o1.member_count;
                }
            });
            TopListAdapter adapter = new TopListAdapter(data);
            adapter.setOnItemClickListener(mOnItemClickCircle);
            adapter.setOnItemChildClickListener(mOnClickJoinListener);
            mRecyclerViewCircleTopList.setLayoutManager(new LinearLayoutManager(CircleToplistActivity.this));
            mRecyclerViewCircleTopList.setAdapter(adapter);
        }

        @Override
        public void onFailed(String errorMsg) {

        }
    };






    // 单击排行榜圈子的处理
    private BaseQuickAdapter.OnItemClickListener mOnItemClickCircle = new BaseQuickAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            Circle circle = (Circle) adapter.getData().get(position);

            Intent intent = new Intent(CircleToplistActivity.this, CircleDetailActivity.class);
            intent.putExtra(CircleDetailActivity.ARG_CIRCLE_ID, circle.id);

            CircleToplistActivity.this.startActivity(intent);
        }
    };

    private BaseQuickAdapter.OnItemChildClickListener mOnClickJoinListener = new BaseQuickAdapter.OnItemChildClickListener() {
        @Override
        public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
            Circle circle = (Circle) adapter.getData().get(position);

            Intent intent = new Intent(CircleToplistActivity.this, CircleDetailActivity.class);
            intent.putExtra(CircleDetailActivity.ARG_CIRCLE_ID, circle.id);

            CircleToplistActivity.this.startActivity(intent);
        }
    };



    class TopListAdapter extends BaseQuickAdapter<Circle, BaseViewHolder> {

        TopListAdapter(@Nullable List<Circle> data) {
            super(R.layout.item_circle_ranking, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Circle item) {
            helper.setText(R.id.tv_ranking_title, item.circle_name);
            helper.setText(R.id.tv_ranking_details, "成员 " + Integer.toString(item.member_count)
                    + " 动态 " + Integer.toString(item.dynamic_count));

            ImageView iv = helper.getView(R.id.iv_ranking_pic);
            GlideApp.with(iv).load(item.head_portrait)
                    .placeholder(R.drawable.user_default_avatar)
                    .error(R.drawable.user_default_avatar)
                    .into(iv);

            int index = helper.getAdapterPosition() + 1;
            helper.setText(R.id.tv_ranking_num, Integer.toString(index));

            helper.addOnClickListener(R.id.tv_ranking_join);
        }
    }
}
